<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>js原型链之方法重写</title>
</head>
<body>
  <ul>
    <li>子类可重写父类方法，同时在子类中利用父类原型调用父类同名方法(不可使用this调用)</li>
  </ul>
  <script>
    function User() {}
    User.prototype.show = function() {
      console.log('user.show')
    }

    function Admin() {}
    // Admin.prototype.__proto__ = User.prototype
    // 通过Object.create()实现继承的顺序至关重要，必须保证定义原型方法或新建对象在Object.create()之后进行, 同时回填constructor
    Admin.prototype = Object.create(User.prototype)
    // Admin.prototype.constructor = Admin
    Object.defineProperty(Admin.prototype, 'constructor', {
      value: Admin,
      enumerable: false
    })
    // 子类方法重写
    Admin.prototype.show = function() {
      User.prototype.show()     // 此时不能使用this.show()
      console.log('admin.show')
    }
    Admin.prototype.role = function() {
      console.log('admin.role')
    }

    const admin = new Admin()
    admin.show()
    admin.role()
  </script>
</body>
</html>